package com.gxa.autoparts.admin.controller;


import com.gxa.autoparts.admin.message.MessageSender;
import com.gxa.autoparts.admin.service.SystemMessageService;
import com.gxa.autoparts.core.common.ExceptionCode;
import com.gxa.autoparts.core.common.JsonResult;
import com.gxa.autoparts.core.exception.BusinessException;
import com.gxa.autoparts.core.pojo.SystemMessage;
import com.gxa.autoparts.core.util.DateUtil;
import com.gxa.autoparts.core.util.JsonUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.jni.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/admin/systemMessage")
@RestController
@Transactional(rollbackFor = Throwable.class)
public class SystemMessageController {

    @Autowired
    SystemMessageService systemMessageService;

    @ApiOperation("查询全部消息")
    @PostMapping("/findAllSystemMessage")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "title", value = "消息标题", required = false),
            @ApiImplicitParam(name = "content", value = "消息内容", required = false),
            @ApiImplicitParam(name = "time1", value = "起始时间", required = false),
            @ApiImplicitParam(name = "time2", value = "截止时间", required = false),
            @ApiImplicitParam(name = "page", value = "页数", required = true),
            @ApiImplicitParam(name = "size", value = "页内数据条数", required = true),
    })
    public Map findAllSystemMessage(String title,String content,String time1,String time2,Integer page,Integer size){

        List<SystemMessage> systemMessages = systemMessageService.selectAllSystemMessage(title, content, time1, time2, (page - 1) * size, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(systemMessages);
        jsonResult.put("totleNumber", systemMessageService.selectAllSystemMessage2(title, content, time1, time2).size());
        jsonResult.setMsg("查询全部消息");
        return jsonResult.getDataMap();
    }

    @ApiOperation("查看详情")
    @PostMapping("/selectSystemMessageById")
    @ApiImplicitParam(name = "id", value = "信息Id", required = true)
    public Map selectSystemMessageById(Long id){
        SystemMessage systemMessage = systemMessageService.selectById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(systemMessage);
        jsonResult.setMsg("查看详情成功");
        return jsonResult.getDataMap();
    }

    @ApiOperation("增加消息")
    @PostMapping("/addSystemMessage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "消息标题", required = false),
            @ApiImplicitParam(name = "content", value = "消息内容", required = false),
    })
    public Map addSystemMessage(String title,String content) throws BusinessException {
        SystemMessage systemMessage = new SystemMessage();
        systemMessage.setContext(content);
        systemMessage.setTitle(title);
        systemMessage.setSendTime(DateUtil.getAfterTenMinutes(new Date()));
        SystemMessage insert = systemMessageService.insert(systemMessage);

        messageSender.send(JsonUtil.ObjectToString(systemMessage),Integer.valueOf(DateUtil.getSecond(insert.getSendTime())));
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("增加消息成功");
        return jsonResult.getDataMap();
    }
    @Autowired
    public MessageSender messageSender;
    //死信队列controller
    @PostMapping("/send")
    public String send(@RequestParam String msg, Integer time) throws BusinessException {
        messageSender.send(msg,time);
        return null;
    }
}
